package com.clt.ess.service.impl;

import com.clt.ess.base.Constant;
import com.clt.ess.entity.SealApply;
import com.clt.ess.entity.User;
import com.clt.ess.service.*;
import com.clt.ess.utils.PowerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

import java.util.List;

import static com.clt.ess.utils.dateUtil.*;
import static com.clt.ess.utils.uuidUtil.getEssUUID;
import static com.clt.ess.utils.uuidUtil.getUUID;

@Service
public class ApplyServiceImpl implements IApplyService {

    @Autowired
    ISealService sealService;
    @Autowired
    ICertificateService certificateService;
    @Autowired
    ISealImgService sealImgService;
    @Autowired
    IUnitService unitService;
    @Autowired
    ILogService logService;


    @Override
    public ModelAndView applyListHandle(HttpSession session, String unitId) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("apply/apply_list");
        //创建申请信息
        SealApply sealApply = new SealApply();
        //申请所属的单位
        sealApply.setUnitId(unitId);
        //申请信息列表里只显示登录人申请的信息
        sealApply.setApplyUserId(PowerUtil.getLoginUser(session).getUserId());
        //类别：提交申请
        sealApply.setApplyState(Constant.SUBMIT_APPLICATION);
        //查出状态位于提交申请的信息
        List<SealApply> sealApplyList = sealService.findSealApply(sealApply);
        //审核驳回的信息
        sealApply.setApplyState(Constant.REVIEW_NO_THROUGH);
        List<SealApply> sealApplyList_no_through = sealService.findSealApply(sealApply);

        //将驳回的信息添加到list里
        sealApplyList.addAll(sealApplyList_no_through);

        modelAndView.addObject("sealApplyList",sealApplyList);
        //单位对象
        modelAndView.addObject("unit",unitService.findUnitById(unitId));
        return modelAndView;
    }

    /**
     * 添加制作印章申请信息
     * @param sealApply 申请信息对象
     * @param session session
     * @return 是否添加成功
     */
    @Override
    public boolean addSealApply(SealApply sealApply, HttpSession session){
        //结果
        boolean result = false;
        //生成申请信息ID
        sealApply.setSealApplyId(getEssUUID(sealApply.getUnitId()));
        //申请信息状态
        sealApply.setApplyState(Constant.SUBMIT_APPLICATION);
        //设置申请人和申请时间
        sealApply.setApplyUserId(PowerUtil.getLoginUser(session).getUserId());
        sealApply.setApplyTime(getDate());
        //设置印章制作单位的id
        sealApply.setMake_unit_id(Constant.make_unit_id);

        User user = PowerUtil.getLoginUser(session);

        String logDetail;
        //为申请的印章生成id
        sealApply.setSealId(getUUID());

        result = sealService.addSealApply(sealApply);

        logDetail = unitService.getUnitNameChain(sealApply.getUnitId())+"向国家烟草专卖局申请制作新印章："+sealApply.getSealName();
        logService.addLogAddSealApplyNew(logDetail, sealApply.getUnitId(),user.getUserId(),"");

        return result;
    }
}
